﻿using System;

namespace Quail
{
    /// <summary>
    /// Basic test shell without an actual backing web browser - useful for non-browser testing
    /// </summary>
    public class EmptyShell : TestShell<EmptyBrowser, WebPage>, IQuailEnvironment
    {
        private readonly string _environmentName;
        private readonly string _startupUrl;

        ///<summary>
        /// Creates a new completely empty shell - usefull for tests where you want to log stuff, but not open a Selenium browser.  You must set DefaultEnvironmentName and DefaultStartupUrl
        /// for this constructor to work properly.
        ///</summary>
        ///<param name="testname"></param>
        ///<param name="owninginstance"></param>
        public EmptyShell(string testname, object owninginstance)
            : this(testname, owninginstance, DefaultEnvironmentName, DefaultStartupUrl)
        {

        }

        ///<summary>
        /// Creates a new completely empty shell - usefull for tests where you want to log stuff, but not open a Selenium browser
        ///</summary>
        ///<param name="testname"></param>
        ///<param name="owninginstance"></param>
        ///<param name="environmentName"></param>
        ///<param name="startupUrl"></param>
        public EmptyShell(string testname, object owninginstance, string environmentName, string startupUrl)
            : base(testname, owninginstance)
        {
            _environmentName = environmentName;
            _startupUrl = startupUrl;
        }

        /// <summary>
        /// Gets or sets the Default EnvironmentName to use for the simple constructor (used by ParallelTestFixture.RunTest)
        /// </summary>
        public static string DefaultEnvironmentName { get; set; }

        /// <summary>
        /// Gets or sets the Default StartupUrl to use for the simple constructor (used by ParallelTestFixture.RunTest)
        /// </summary>
        public static string DefaultStartupUrl { get; set; }

        public string GetEnvironmentName()
        {
            return _environmentName;
        }

        public override ITestShell CreateNewTestShell(string testName)
        {
            return new EmptyShell(testName ?? TestName, this, _environmentName, _startupUrl);
        }

        public override string GetStartupUrl()
        {
            return _startupUrl;
        }

        /// <summary>
        /// Starts a new Empty test shell
        /// </summary>
        /// <param name="testname">the name of the test</param>
        /// <param name="owninginstance">The object that is creating the EmptyShell (for logging purposes)</param>
        /// <param name="environmentName"></param>
        /// <param name="startupUrl"></param>
        /// <returns>A new EmptyShell</returns>
        public static EmptyShell Start(string testname, object owninginstance, string environmentName, string startupUrl)
        {
            var shell = new EmptyShell(testname, owninginstance, environmentName, startupUrl);
            try
            {
                shell.Start();
                return shell;
            }
            catch (Exception)
            {
                shell.Dispose();
                throw;
            }
        }

        protected override TestSession CreateNewTestSession()
        {
            return new EmptySession(TestName);
        }

        protected override void AddLoggerFactories()
        {
            // maybe add code here to turn off DB logging;  if (SAJRepositorySettings.Default.LogToDatabase)
            TestSession.RootTask.Logger.TaskFactories.Add(new DbLogger.QuailDbLogger(AssemblyName, ClassName, this));
        }
    }
}
